/*
 * Title:        CloudScheduler Toolkit
 * Description:  a Toolkit for Modeling and Simulation of Job Scheduling and Resource Provisioning in Cloud System
 * Licence:      GPL - http://www.gnu.org/copyleft/gpl.html
 * Author:       Fuhui Wu
 *
 * Copyright (c) 2013-2014, The National University of Defense Technology, China
 */

package org.nudt.jCloud.example.resourceStealing1;

import java.util.ArrayList;
import java.util.List;

import org.nudt.jCloud.infrastructure.Slot;
import org.nudt.jCloud.infrastructure.SlotType;
import org.nudt.jCloud.resourceScaler.ResourceScaler;
import org.nudt.jCloud.simulation.RequestData;
import org.nudt.jCloud.simulation.ScaleData;

public class BatchScaler extends ResourceScaler{
	private SlotType slotType;
	private int slotNum;
	private boolean scaled;
	private List<Slot> stolenSlotList;
	
	public BatchScaler(SlotType slotType, int slotNum){
		super();
		setSlotType(slotType);
		setSlotNum(slotNum);
		scaled = false;
		stolenSlotList = new ArrayList<Slot>();
	}	

	@Override
	public void specialReset() {
		scaled = false;	
		stolenSlotList.clear();
	}

	@Override
	public void scaleResource(RequestData requestData, ScaleData scaleData) {
		if(scaled == false){
			scaleData.getToLeaseSlot().put(slotType, slotNum);			
			setScaled(true);
		}		
	}
	
	public void stealResource(List<Slot> stealingSlotList){
		getStolenSlotList().addAll(stealingSlotList);
		getFrwk().leaseSlot(slotType, stealingSlotList);
	}
	
	public void preemptSlot(List<Slot> preemptSlotList) {
		getStolenSlotList().removeAll(preemptSlotList);
		if(getFrwk().getFreeSlots().containsKey(slotType)){
			getFrwk().getFreeSlots().get(slotType).removeAll(preemptSlotList);
			if(getFrwk().getFreeSlots().get(slotType).size()<=0){
				getFrwk().getFreeSlots().remove(slotType);
			}
		}
	}
	
	public int getSlotNum(){
		return slotNum;
	}
	
	public void setSlotNum(int slotNum){
		this.slotNum = slotNum;
	}
	
	public boolean isScaled(){
		return scaled;
	}
	
	public void setScaled(boolean scaled){
		this.scaled = scaled;
	}

	public SlotType getSlotType() {
		return slotType;
	}

	public void setSlotType(SlotType slotType) {
		this.slotType = slotType;
	}

	public List<Slot> getStolenSlotList() {
		return stolenSlotList;
	}

	public void setStolenSlotList(List<Slot> stolenSlotList) {
		this.stolenSlotList = stolenSlotList;
	}
	
}
